package com.csii.pos.cashdaily.service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csii.adhoc.exception.ParamValidityException;
import com.csii.adhoc.util.ConvertUtils;
import com.csii.pos.caacctewsu.CaAcctEwSu;
import com.csii.pos.cashdaily.CashCurBlc;
import com.csii.pos.cashdaily.mapper.CashCurBlcMapper;
import com.csii.pos.cashdaily.service.CashCurBlcService;
import com.csii.pos.rpc.CashResponse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description:
 * @author:
 * @create:
 */
@Service
public class CashCurBlcServiceImpl implements CashCurBlcService {
    @Resource
    private CashCurBlcMapper cashCurBlcMapper;

    @Transactional
    @Override
    public IPage<CashCurBlc> saveCashCurBlc(Page<CashCurBlc> page, List<CashResponse> cashResponses) {
        int insert=0;
        for (CashResponse c:cashResponses) {
            CashCurBlc convert = ConvertUtils.convert(CashCurBlc.class, c);
            int i = cashCurBlcMapper.insertTableName(convert);
            insert=insert+i;
        }
        if (insert>0){
            cashCurBlcMapper.deleteCashCurBlc();
            for (CashResponse c:cashResponses) {
                CashCurBlc convert = ConvertUtils.convert(CashCurBlc.class, c);
                cashCurBlcMapper.insertCashCurBlc(convert);
            }
        }else {
            throw new ParamValidityException("数据为空或者数据格式异常!");
        }
        IPage<CashCurBlc> cashCurBlcIPage = cashCurBlcMapper.pageCashCurBlc(page,new CashCurBlc());
        return cashCurBlcIPage;
    }

    @Override
    public IPage<CashCurBlc> pageCashCurBlc(Page<CashCurBlc> page, CashCurBlc cashCurBlc) {
        IPage<CashCurBlc> cashCurBlcIPage = cashCurBlcMapper.pageCashCurBlc(page,cashCurBlc);
        return cashCurBlcIPage;
    }
}
